package com.aike.service;

import com.aike.dao.SeatMapper;
import com.aike.pojo.Meta;
import com.aike.pojo.MySeat;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class SeatServiceImpl implements SeatService {

    private SeatMapper seatMapper;

    public void setSeatMapper(SeatMapper seatMapper) {
        this.seatMapper = seatMapper;
    }

    public String reserveSeat(Integer personId, Integer seatId) {

        JSONObject jsonObject = new JSONObject();
        MySeat mySeat = seatMapper.selectMySeat(personId);
        if(mySeat != null)
        {
            //代表自己已经占有了位置
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(400, "已经占用了一个位置"));
        }
        else
        {
            int v = seatMapper.reserveSeat(personId, seatId);
            if(v > 0)
            {
                jsonObject.put("data", "null");
                jsonObject.put("meta", new Meta(200, "占座成功"));
            }
            else
            {
                jsonObject.put("data", "null");
                jsonObject.put("meta", new Meta(400, "占座失败"));
            }
        }
        return jsonObject.toJSONString();
    }

    public String selectMySeat(Integer personId) {
        JSONObject jsonObject = new JSONObject();
        MySeat mySeat = seatMapper.selectMySeat(personId);
        if(mySeat == null)
        {
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(200, "查询成功"));
        }
        else
        {
            jsonObject.put("data", mySeat);
            jsonObject.put("meta", new Meta(200, "查询成功"));
        }
        return jsonObject.toJSONString();
    }

    public String releaseSeat(Integer personId) {
        JSONObject jsonObject = new JSONObject();
        int v = seatMapper.releaseSeat(personId);
        if(v > 0)
        {
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(200, "取消占座成功"));
        }
        else
        {
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(400, "取消占座失败"));
        }
        return jsonObject.toJSONString();
    }

    //回到位置
    public String returnSeat(Integer personId)
    {
        JSONObject jsonObject = new JSONObject();
        int v = seatMapper.returnSeat(personId);
        if(v > 0)
        {
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(200, "回到座位成功"));
        }
        else
        {
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(400, "回到座位失败"));
        }
        return jsonObject.toJSONString();
    }


    //暂离位置
    public String pauseSeat(Integer personId)
    {
        JSONObject jsonObject = new JSONObject();
        int v = seatMapper.pauseSeat(personId);
        if(v > 0)
        {
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(200, "暂离座位成功"));
        }
        else
        {
            jsonObject.put("data", "null");
            jsonObject.put("meta", new Meta(400, "暂离座位失败"));
        }
        return jsonObject.toJSONString();
    }
}
